<?php
$event_id = $_REQUEST['event_id'];

if ($_POST['Submit']) {
	$question = $_POST['question'];
	$question_type = $_POST['question_type'];
	$values = $_POST['values'];
	$required = $_POST['required'] ? 'Y':'N';
	$sequence = $wpdb->get_var("SELECT max(sequence) FROM $em_question_table where event_id = '$event_id'") + 1;

	$wpdb->query("INSERT INTO $em_question_table (`event_id`, `sequence`, `question_type`, `question`, `values`, `required`)"
				. " values('$event_id', '$sequence', '$question_type', '$question', '$values', '$required')");
}
else if ($_GET['delete'])
{
	$question_id = $_GET['question_id'];
	$wpdb->query("DELETE from $em_question_table where id = '$question_id'");
}

$event = $wpdb->get_row("SELECT * from $em_event_table where id = '$event_id'");

?>

<div class="wrap">
<h2>Event Questions - <?php echo $event->event_name ?></h2>
<h3><a href="<?php echo EM_URL ?>event-list.php">&laquo; Event List</a> </h3>
<p>Add additional questions to the registration form below.  By default all participants will be
asked for their email address, first name, last name, street address, city, state, zip code, and phone number.</p>

<ul id="question_list" class="sort_list">
	<?php

	$questions = $wpdb->get_results("SELECT * from $em_question_table where event_id = $event_id order by sequence");
	if ($questions) {
	foreach ($questions as $question) {
	?>
	<li id="question_<?php echo $question->id ?>" class="handle">
		<p><strong><?php echo $question->question ?></strong>
		<a href="<?php echo EM_URL ?>edit-question.php&amp;question_id=<?php echo $question->id ?>">edit</a> |
		<a href="<?php echo EM_URL ?>edit-questions.php&amp;event_id=<?php echo $event_id ?>&amp;delete=true&amp;question_id=<?php echo $question->id ?>" onclick="return confirm('Are you sure you want to delete this question?')">delete</a></p>
	</li>
	<?php }	} ?>
</ul>
<script type="text/javascript">
<!--
	function updateQuestions() {
		var ajaxParms = 'action=order_questions&event_id=<?php echo $event_id ?>&sequence=' + Sortable.sequence('question_list');
		new Ajax.Request('<?php echo EM_BASE_URL . "ajax-actions.php";  ?>', {method: 'post', parameters: ajaxParms, onFailure: showResponse});
	}

	function showResponse() {
		alert("Your change failed.");
	}

	Sortable.create('question_list', {onUpdate: function() { updateQuestions();} });
-->
</script>

<h2>Add New Question</h2>
<p>Enter a new question below.  The type refers to the way a participant can respond to the question.  Text will allow a single line
of text to be entered, where text area will allow multiple lines.  Single and multiple will prompt the participant with choices.  Single will
only allow one response, and multiple will allow multiple responses.  When using single or multiple, enter the possible values in the value box
separated by commas.</p>
<form name="form1" method="post" action="">
<input type="hidden" name="event_id" value="<?php echo $event_id ?>"/>
<table width="100%" cellspacing="2" cellpadding="5" class="editform">
<tr valign="top">
	<th width="33%" scope="row">Question:</th>
	<td><input name="question" type="text" id="question" size="50" value=""/></td>
</tr>
<tr valign="top">
	<th width="33%" scope="row">Type:</th>
	<td>
		<select name="question_type" id="question_type">
			<option value="TEXT">Text</option>
			<option value="TEXTAREA">Text Area</option>
			<option value="SINGLE">Single</option>
			<option value="MULTIPLE">Multiple</option>
		</select>
	</td>
</tr>
<tr valign="top">
	<th width="33%" scope="row">Values:</th>
	<td><input name="values" type="text" id="values" size="50" value=""/></td>
</tr>
<tr valign="top">
	<th width="33%" scope="row">Required:</th>
	<td><input name="required" type="checkbox" id="required" /></td>
</tr>
</table>

<p class="submit">
<input type="submit" name="Submit" value="<?php _e('Add Question') ?> &raquo;" />
</p>
</form>

</div>
